/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Bach Ngoc  Toan
 */
public class Configs {

    public static final String CONFIG_FILENAME = "configs.properties";
    public static final String CONFIG_SERVERNAME = "serverName";
    public static final String CONFIG_PORTNUMBER = "port";
    public static final String CONFIG_DATABASENAME = "databaseName";
    public static final String CONFIG_SERVERUSERNAME = "userName";
    public static final String CONFIG_SERVERPASSWORD = "password";
    private static Properties p;
    
    public static final String DEFAULT_ENCODING = "UTF-8";
    static BASE64Encoder enc = new BASE64Encoder();
    static BASE64Decoder dec = new BASE64Decoder();
    static String key = "key phrase used for XOR-ing";

    /**
     * get property folow by name property
     * @param prop
     * @return value of property
     */
    public static String getProperty(String prop) {
        if(p == null) {
            return "";
        }

        return p.getProperty(prop);
    }

    /**
     * Set property folow by name of property and value
     * @param prop
     * @param value
     */
    public static void setProperty(String prop, String value) {
        if(p == null) {
            p = new Properties();
        }
        p.setProperty(prop, value);
    }

    /**
     * Load all property from file config. if load config success then
     * return true else return falase
     * @return boolean
     */
    public static boolean loadConfigurations() {
        p = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(CONFIG_FILENAME);
            p.load(fis);
            p.setProperty(CONFIG_SERVERPASSWORD, base64decode(p.getProperty(CONFIG_SERVERPASSWORD)));
            fis.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }

        return true;
    }

    /**
     * set all property into file configs
     */
    public static void saveConfiguration() {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(CONFIG_FILENAME);
            String serverPass = p.getProperty(CONFIG_SERVERPASSWORD);
            p.setProperty(Configs.CONFIG_SERVERPASSWORD, base64encode(serverPass));
            p.store(fos, "-----Expo Management System Configuration File------");
            p.setProperty(Configs.CONFIG_SERVERPASSWORD, serverPass);
            fos.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
    }


    /**
     * Encrypt a string a return string was be encrypted
     * @param text
     * @return a String
     */
    public static String base64encode(String text) {
        try {
            text = xorMessage(text, key);
            String rez = enc.encode(text.getBytes(DEFAULT_ENCODING));
            return rez;
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }//base64encode

    /**
     * Decrypt a string a return string was be decrypt
     * @param text
     * @return a String
     */
    public static String base64decode(String text) {

        try {
            text = new String(dec.decodeBuffer(text), DEFAULT_ENCODING);
            return xorMessage(text, key);
        } catch (IOException e) {
            return null;
        }

    }//base64decode

    public static String xorMessage(String message, String key) {
        try {
            if (message == null || key == null) {
                return null;


            }
            char[] keys = key.toCharArray();
            char[] mesg = message.toCharArray();

            int ml = mesg.length;
            int kl = keys.length;
            char[] newmsg = new char[ml];

            for (int i = 0; i < ml; i++) {
                newmsg[i] = (char) (mesg[i] ^ keys[i % kl]);
            }//for i
            mesg = null;
            keys = null;
            return new String(newmsg);
        } catch (Exception e) {
            return null;
        }
    }//xorMessage
}
